﻿@model ShoppingCartModel.GiftCardBoxModel
@if (Model.Display)
{
    <div class="giftcard-box">
        <div class="title">
            <strong>@T("ShoppingCart.GiftCardCouponCode")</strong>
        </div>
        <div class="coupon-code">
            <div class="form-group">
                <span class="col-form-label">@T("ShoppingCart.GiftCardCouponCode.Tooltip")</span>
                <div class="input-group">
                    <input id="giftcardcouponcode" name="giftcardcouponcode" type="text" class="form-control gift-card-coupon-code" />
                    <label for="giftcardcouponcode" class="sr-only">@T("ShoppingCart.GiftCardCouponCode.Button")</label>
                    <span class="input-group-append">
                        <button type="button" name="applygiftcardcouponcode" id="applygiftcardcouponcode" class="btn btn-outline-secondary apply-discount-coupon-code-button"
                                data-href="@Url.RouteUrl("ApplyGiftCard")">
                            @T("ShoppingCart.GiftCardCouponCode.Button")
                        </button>
                    </span>
                </div>
            </div>
        </div>
        <script asp-location="Footer">
            $(document).ready(function () {
                $('#giftcardcouponcode').keydown(function (event) {
                    if (event.keyCode == 13) {
                        $('#applygiftcardcouponcode').trigger("click")
                        return false;
                    }
                });
            });
        </script>
        @if (!String.IsNullOrEmpty(Model.Message))
        {
            <div class="card border-0 text-white @(Model.IsApplied ? "card-success message-success" : "card-danger message-failure") text-center">
                <div class="card-body p-3">
                    <blockquote class="card-bodyquote mb-0">
                        <span>@Model.Message</span>
                    </blockquote>
                </div>
            </div>
        }
    </div>
    <script asp-location="Footer">
        $(document).ready(function () {
            $("#applygiftcardcouponcode").click(function () {
                var href = $(this).closest('[data-href]').data('href');
                var giftCardData = {
                    giftcardcouponcode: $('#giftcardcouponcode').val(),
                };
                addAntiForgeryToken(giftCardData);
                $.ajax({
                    cache: false,
                    type: "POST",
                    data: giftCardData,
                    url: href,
                    success: function (data) {
                        $('#ordersummarypagecart').replaceWith(data.cart);
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        alert('Failed to retrieve Shopping Cart Page.');
                    }
                });
            });
        });
    </script>
}